﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using MoreMountains.Tools;
public class BattleManager : MMSingleton<BattleManager>
{
    public CharacterInfo[] PlayerTeam;
    public CharacterInfo[] EmenyTeam;

    public List<Character> playerT;
    public List<Character> enemyT;
    /// <summary>
    /// 保存战斗信息
    /// </summary>
    public List<BattleInfo> attackInfoList;

    /// <summary>
    /// 当前活动的队伍 0 --玩家  1--敌人
    /// </summary>
    int activeTeam = 0;
    /// <summary>
    /// 当前活动的角色的索引
    /// </summary>
    int activeCharacterIndex;
    /// <summary>
    /// 组装角色
    /// </summary>
    public void SetupCharacter()
    {
        playerT = new List<Character>();
        enemyT = new List<Character>();
        foreach (var item in PlayerTeam)
        {
            Character c = new Character(item.characterName, item.maxHp, item.maxMp,item.animatorController,item.attacks);
            playerT.Add(c);
        }

        foreach (var item in EmenyTeam)
        {
            Character c = new Character(item.characterName, item.maxHp, item.maxMp,item.animatorController,item.attacks);
            enemyT.Add(c);
        }

    }

    public void Run()
    {
        List<Character> currentT;
        List<Character> targetT;
        attackInfoList = new List<BattleInfo>();
        while (GetAliveCharacterNumber(playerT) > 0 && GetAliveCharacterNumber(enemyT) > 0)
        {
            currentT = activeTeam == 0 ? playerT : enemyT;
            targetT = activeTeam == 1 ? playerT : enemyT;
            //print("=========当前攻击队伍:"+activeTeam);
            foreach (var i in currentT)
            {
                if (!i.alive)
                {
                    continue;
                }
                Character target = null;
                foreach (var t in targetT)
                {
                    if (t.alive)
                    {
                        target = t;
                        break;
                    }
                }
                if (target != null)
                {
                    i.Attack(target);
                    attackInfoList.Add(new BattleInfo(i, target));
                }
            }
            activeTeam = activeTeam == 0 ? 1 : 0;
        }
        if (playerT.Count > 0)
        {
            // print("Player Win");
        }
        else
        {
            // print("Enemy Win");
        }


    }

    public int GetAliveCharacterNumber(List<Character> list)
    {
        int num = 0;
        foreach (var i in list)
        {
            if (i.alive)
            {
                num++;
            }
        }
        return num;
    }

    private void Start()
    {
        SetupCharacter();
        Run();
        DisplayBattle();
    }

    private void DisplayBattle()
    {
/*        foreach (var i in attackInfoList)
        {
            print(i);
        }*/
        BattleDisplay.Instance.RunDisplay();
    }
}
/// <summary>
/// 记录攻击信息
/// </summary>
public class BattleInfo
{
    /// <summary>
    /// 攻击者
    /// </summary>
    public Character attacker;
    /// <summary>
    /// 攻击目标
    /// </summary>
    public Character attackTarget;
    /// <summary>
    /// 产生的攻击值
    /// </summary>
    public float attackValue;
    /// <summary>
    /// 使用的攻击的名字
    /// </summary>
    public int attackID;
    /// <summary>
    /// 目标当前是否死亡
    /// </summary>
    public bool targetCurrentAlive;

    public BattleInfo(Character attacker, Character attackTarget)
    {
        this.attacker = attacker;
        this.attackTarget = attackTarget;
        targetCurrentAlive = attackTarget.alive;
        attackValue = attacker.lastAttackValue;
        attackID = attacker.lastAttackID;
        
    }

    public override string ToString()
    {
        return string.Format("{0}={1}=>{2},产生伤害:{3}", attacker.name, attackID, attackValue, attackTarget.name);
    }
}

